|
1
|
|
|
/** |
|
2
|
|
|
* Rich Variables plugin for Craft CMS |
|
3
|
|
|
* |
|
4
|
|
|
* Rich Variables JS |
|
5
|
|
|
* |
|
6
|
|
|
* @author nystudio107 |
|
7
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
8
|
|
|
* @link https://nystudio107.com |
|
9
|
|
|
* @package RichVariables |
|
10
|
|
|
* @since 1.0.18 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
(function($R) |
|
|
|
|
|
|
14
|
|
|
{ |
|
|
|
|
|
|
15
|
|
|
$R.add('plugin', 'richvariables', { |
|
|
|
|
|
|
16
|
|
|
translations: { |
|
17
|
|
|
en: { |
|
18
|
|
|
"variables": "Variables" |
|
19
|
|
|
} |
|
20
|
|
|
}, |
|
21
|
|
|
init: function(app) |
|
|
|
|
|
|
22
|
|
|
{ |
|
|
|
|
|
|
23
|
|
|
this.app = app; |
|
24
|
|
|
this.lang = app.lang; |
|
25
|
|
|
this.inline = app.inline; |
|
26
|
|
|
this.toolbar = app.toolbar; |
|
27
|
|
|
this.insertion = app.insertion; |
|
28
|
|
|
|
|
29
|
|
|
// Grab the globals set Reference Tags from our controller |
|
30
|
|
|
var request = new XMLHttpRequest(); |
|
31
|
|
|
request.open('GET', Craft.getActionUrl('rich-variables'), false); |
|
|
|
|
|
|
32
|
|
|
request.onload = function() { |
|
|
|
|
|
|
33
|
|
|
if (request.status >= 200 && request.status < 400) { |
|
|
|
|
|
|
34
|
|
|
} else { |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
}; |
|
37
|
|
|
request.send(); |
|
38
|
|
|
this.request = request; |
|
39
|
|
|
}, |
|
40
|
|
|
start: function() |
|
|
|
|
|
|
41
|
|
|
{ |
|
|
|
|
|
|
42
|
|
|
var dropdown = {}; |
|
43
|
|
|
var responseVars = JSON.parse(this.request.responseText); |
|
44
|
|
|
|
|
45
|
|
|
// Iterate through each menu item, adding them to our dropdown |
|
46
|
|
|
responseVars.variablesList.forEach(function(menuItem, index) { |
|
|
|
|
|
|
47
|
|
|
var key = 'point' + (index + 1); |
|
48
|
|
|
var refTag = '<ins>' + menuItem.text + '</ins>'; |
|
49
|
|
|
dropdown[key] = { |
|
50
|
|
|
title: menuItem.title, |
|
51
|
|
|
api: 'plugin.richvariables.insert', |
|
52
|
|
|
args: refTag |
|
53
|
|
|
}; |
|
54
|
|
|
}); |
|
|
|
|
|
|
55
|
|
|
// Handle empty menu items |
|
56
|
|
|
if (responseVars.variablesList.length === 0) { |
|
57
|
|
|
dropdown.point1 = { |
|
58
|
|
|
title: "No Globals Found", |
|
59
|
|
|
func: function(buttonName) { |
|
|
|
|
|
|
60
|
|
|
// NOP |
|
61
|
|
|
}, |
|
62
|
|
|
}; |
|
63
|
|
|
} |
|
64
|
|
|
// Add the button and dropdown |
|
65
|
|
|
var $button = this.toolbar.addButton('variables', { title: this.lang.get('variables') }); |
|
66
|
|
|
$button.setDropdown(dropdown); |
|
67
|
|
|
if (responseVars.useIconForMenu) { |
|
68
|
|
|
$button.setIcon('<img src="' + responseVars.menuIconUrl + '" height="16" width="16" style="margin-top: -2px;">'); |
|
69
|
|
|
} |
|
70
|
|
|
}, |
|
71
|
|
|
insert: function(refTag) |
|
|
|
|
|
|
72
|
|
|
{ |
|
|
|
|
|
|
73
|
|
|
this.insertion.insertRaw(refTag); |
|
74
|
|
|
} |
|
75
|
|
|
}); |
|
|
|
|
|
|
76
|
|
|
})(Redactor); |
|
|
|
|
|
|
77
|
|
|
|